Tack.Designer.Condition = new Class({
	initialize: function (designer, condition) {
		this.html = {
			main: new Element('li', { 'class': 'condition' }),
			title: new Element('span'),
			inputs: []
		};

		this.condition = condition;
		this.designer = designer;

		$each(designer.conditions, function (type, name) {
			if (condition instanceof type) {
				this.html.title.set('text', name);

				var a = type.prototype.args;
				for (var p in a) {
					var input, s = a[p], t = $type(s), v = condition[p];
					switch (t) {
						case 'array':
						case 'function':
							if (t == 'function') s = s.apply(this.designer.tack.game);
							input = new Element('select');

							s.each(function (value) {
								var option = new Element('option', { text: value, value: value });
								if (value == v) option.set('selected', 1);
								input.adopt(option);
							});

							break;
						case 'string':
							switch (s) {
								case 'longtext':
									input = new Element('textarea', { cols: 50, rows: 5, text: v });
									break;
								case 'number':
									input = new Element('input', { size: 5, value: v });
									break;
								case 'text':
									input = new Element('input', { value: v });
									break;
								default:
									return;
							}

							break;
						
						default:
							return;
					}

					this.html.inputs.push(input);

					input.addEvent('change', (function (e, item, prop) {
						item[prop] = this.value;
					}).bindWithEvent(input, [condition, p]));

					input.fireEvent('change');
				}
			}
		}, this);

		if (condition.negate) this.html.main.addClass('negate');
		this.html.main.adopt(this.html.title, this.html.inputs);

		this.designer.conditionMenu.attach(this.html.title, this);
	},

	remove: function () {
		this.condition.remove();
		this.html.main.destroy();
	},

	toggleNegation: function () {
		this.condition.negate = !this.condition.negate;
		this.html.main.toggleClass('negate');
	}
});
